import request from '@/utils/request';
import { HTTP_METHOD } from '@/utils/http';

const base_url = '/template';

/**
 * @export 获取表单基础模板
 * @param {*} tid
 * @return {*}
 */
export function getTemplateBaseFields(tid) {
  return request({
    url: `${base_url}/field/${tid}/base`,
    method: HTTP_METHOD.GET
  });
}

/**
 * @export 获取表单分组模板
 * @param {*} tid
 * @return {*}
 */
export function getTemplateGroupFields(tid, fid) {
  return request({
    url: `${base_url}/field/${tid}/${fid}/group`,
    method: HTTP_METHOD.GET
  });
}

/**
 * 表单
 * @export 获取表单模板
 * @return {*} task_id
 */
export function getTemplateFields(task_id) {
  return request({
    url: `${base_url}/fields/${task_id}`,
    method: HTTP_METHOD.GET
  });
}

// export function getTemplateValueFields(fid) {
//   return request({
//     url: `${base_url}/select/value/${fid}`,
//     method: HTTP_METHOD.GET
//   });
// }

/**
 * @export 获取远程工单数据
 * @param {*} url 地址
 * @param {*} data 数据
 * @return {*}
 */
export function getTemplatRemote(url, data) {
  // 关联工单
  const WORK_URL = `${base_url}/number`;

  // 部门
  const SELECT_URL = `${base_url}/select/value`;

  const { inputValue, vid, fid } = data;

  switch (url) {
    // 关联工单
    case WORK_URL:
      return request({
        url,
        method: HTTP_METHOD.GET,
        params: {
          number__istartswith: inputValue,
          fid
        }
      });

    // select
    case SELECT_URL:
      return request({
        url,
        method: HTTP_METHOD.GET,
        params: {
          fid: inputValue,
          vid
        }
      });

    default:
      return new Promise((r) => r());
  }
}

/**
 * @export 获取预览文件base64
 * @param {string} [filename='http://dev.inhoo.net.cn/upload/1625038131.docx'] 文件名称
 * @param {string} [department='技术部'] 部门
 * @return {*}
 */
export function getPreviewFileBase64(filename, safe) {
  return request({
    url: `${base_url}/accessory_preview`,
    method: HTTP_METHOD.GET,
    params: {
      filename,
      safe
    }
  });
}
